import { Rule } from 'ant-design-vue/lib/form';
import { ref } from 'vue'

export function useEmail()  {
  //- 定义-邮箱响应式数据
  const emailStrRef = ref<string>("");

  //- 方法-验证邮箱
  const validateEmail = async (_rule: string, _value: string) => {
    // 1.判断是否为空值
    if (_value === "") {
      return Promise.reject("邮箱不能为空");
    }
    // 2.判断邮箱格式是否正确
    const reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
    if (!_value.match(reg)) {
      return Promise.reject("邮箱格式不正确");
    }
    return Promise.resolve();
  };

  //- 邮箱校验规则
  const emailRulesArr: Array<Object> = [
    { required: true, validator: validateEmail }
  ];

  //- 返回暴露数据
  return {
    emailStrRef,
    emailRulesArr,
  }
}